<template>
  <div
      style="height: 100vh; display: flex;  align-items: center; justify-content: center;background-color: rebeccapurple">
    <div style="display: flex;background-color: white;width: 60%;border-radius: 10px;overflow: hidden">
      <div style="flex: 1;display: flex;align-items: center;justify-content: center;">
        <img src="@/assets/login.jpg" alt="login" style="width: 60% ;height: 100%"/>
      </div>
      <div style="flex: 1;display: flex;align-items: center;justify-content: center;">
        <el-form :model="user" style="width: 80% " :rules="rules" ref="loginRef">
          <div style="font-size: 20px; font-weight: bold;text-align: center">欢迎登录后台管理系统</div>

          <el-form-item prop="username">
            <el-input prefix-icon="el-icon-user" size="medium" v-model="user.username"
                      placeholder="请输入账号"></el-input>
          </el-form-item>

          <el-form-item prop="password">
            <el-input prefix-icon="el-icon-lock" size="medium" v-model="user.password"
                      placeholder="请输入密码"></el-input>
          </el-form-item>
          <el-form-item prop="code">
            <div style="display: flex">
              <el-input placeholder="请输入验证码" prefix-icon="el-icon-circle-check" size="medium" style="flex: 1" v-model="user.code"></el-input>
              <div style="flex: 1; height: 36px">
                <valid-code @update:value="updateCode" />
              </div>
            </div>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" style="width: 100%" @click="login">登录</el-button>
          </el-form-item>
          <div style="display: flex">
            <div style="flex: 1">还没有账号？请<span style="color: blue;cursor: pointer" @click="$router.push('/register')">注册</span></div>
            <div style="flex: 1;text-align:right"><span style="color: red; cursor: pointer" @click="$router.push('/forgetPassword')">忘记密码？</span></div>
          </div>
        </el-form>
      </div>
    </div>
  </div>
</template>
<script>

import request from "@/utils/request";
import ValidCode from "@/conponents/ValidCode.vue";

export default {
  name: "login",
  components: {
    ValidCode
  },
  data() {
    // 验证码(错误)
    const validateCode = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入验证码'));
      } else if (value.toLowerCase() !== this.code.toLowerCase()) {
        callback(new Error('验证码不正确'));
      } else {
        callback();
      }
    };

    return {
      code: "", // 组件传过来的验证码
      user: {
        code: "", // 表单输入的验证码
        username: "",
        password: ""
      },
      rules: {
        username: [
          {required: true, message: '请输入账号', trigger: 'blur'},
        ],
        password: [
          {required: true, message: '请输入密码', trigger: 'blur'},
        ],
        code: [
          {validator: validateCode, trigger: 'blur'}// 自定义验证器
        ],
      }
    }
  },

  methods: {
    updateCode(code) {
      this.code = code.toLowerCase()
    },
    login() {
      this.$refs['loginRef'].validate((valid) => {
        if (valid) {
          // 验证通过
          this.$request.post('/login', this.user).then(res => {
            if (res.code === '200') {
              this.$router.push('/')
              this.$message.success('登录成功')
              localStorage.setItem("honey-user", JSON.stringify(res.data))  // 存储用户数据
            } else {
              this.$message.error("账号或密码错误！")
            }
          })
        }
      })
    }
  }
}
</script>
<style scoped>
</style>
